package com.hmall.gateway.filter;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Acer
 */
@Component
@Slf4j
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<PrintAnyGatewayFilterFactory.Config> {
// 核心方法
     @Override
    public GatewayFilter apply(Config config) {
        return ((exchange, chain) -> {
            ServerHttpResponse response = exchange.getResponse();
            log.info("用户:{},城市:{},性别:{}",config.getName(),config.getCity(),config.getGender());
            log.info("路由过滤器执行了");
            return chain.filter(exchange);
        });
    }
//配置类
    @Data
    static class Config{
        private String name;
        private String city;
        private String gender;
    }
//    返回变量名称（配置映射方法）
    @Override
    public List<String> shortcutFieldOrder(){
        return List.of("name","city","gender");
    }
//    返回当前配置类的类型
    @Override
    public Class<Config> getConfigClass(){
        return Config.class;
    }
}
